﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace shuju_jiange
{
    public partial class FrmOrderTime : Form
    {
        private const string strOrderTimeFileName = "OrderTime.dat";
        public FrmOrderTime()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<uint> orders = new List<uint>();
            string[] strOrders = tbOrderTime.Text.Trim().Split(' ');

            foreach(string str in strOrders)
            {
                uint order;
                if (uint.TryParse(str, out order))
                {
                    orders.Add(order);
                }
            }
            if (orders.Count > 0)
            {
                string strOrderTime = "";
                foreach (uint i in orders)
                {
                    strOrderTime += i +" ";
                }
                lbOrderTimes.Items.Add(strOrderTime);
            }
        }

        private void btnDelSelection_Click(object sender, EventArgs e)
        {
            object[] selObjects = new object[lbOrderTimes.SelectedItems.Count];
            lbOrderTimes.SelectedItems.CopyTo(selObjects, 0);
            foreach (object item in selObjects)
            {
                lbOrderTimes.Items.Remove(item);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strOrderTimeFilePath = Application.StartupPath + "//" + strOrderTimeFileName;

            try
            {
                if (File.Exists(strOrderTimeFilePath))
                {
                    File.Delete(strOrderTimeFilePath);
                }
                List<string> strs = new List<string>();
                foreach (object item in lbOrderTimes.Items)
                {
                    strs.Add(item.ToString());
                }
                File.WriteAllLines(strOrderTimeFilePath, strs);
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch
            {
                MessageBox.Show("文件保存失败。", "提示");
            }
        }

        private void FrmOrderTime_Load(object sender, EventArgs e)
        {
            // 加载顺序次数数据
            LoadrOrderTime();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            lbOrderTimes.Items.Clear();
            LoadrOrderTime();
        }
        /// <summary>
        /// 加载顺序次数数据
        /// </summary>
        private void LoadrOrderTime()
        {
            string strOrderTimeFilePath = Application.StartupPath + "//" + strOrderTimeFileName;
            if (File.Exists(strOrderTimeFilePath))
            {
                try
                {
                    string[] strOrderTimes = File.ReadAllLines(strOrderTimeFilePath);
                    lbOrderTimes.Items.AddRange(strOrderTimes);
                }
                finally
                {

                }
            }
        }
    }
}
